﻿using DADAL;
using Microsoft.IdentityModel.Tokens;

namespace DABLL
{
    public class Project
    {
        /// <summary> 添加项目
        /// 
        /// </summary>
        /// <param name="obj"></param>
        public static Response AddProject(ProjectModel obj)
        {   
            Response response = Response.DefaultResponse();
            if (obj.Number.IsNullOrEmpty())
            {
                response.Msg = "请输入工程编号";
                return response;
            }
            if (obj.Name.IsNullOrEmpty())
            {
                response.Msg = "请输入工程名称";
                return response;
            }
            if (obj.StakeMark.IsNullOrEmpty())
            {
                response.Msg = "请输入工程桩号";
                return response;
            }
            if (obj.PipeSpecification.IsNullOrEmpty())
            {
                response.Msg = "请输入管材规格";
                return response;
            }
            if (obj.PipeLength.IsNullOrEmpty())
            {
                response.Msg = "请输入正确的管道长度";
                return response;
            }
            if (obj.Medium.IsNullOrEmpty())
            {
                response.Msg = "请输入试压介质";
                return response;
            }
            if (obj.DesignPressure <= 0)
            {
                response.Msg = "请输入正确的设计压力";
                return response;
            }

            if (obj.PipeNature.IsNullOrEmpty())
            {
                response.Msg = "请选择管道性质";
                return response;
            }
            if (obj.TestType.IsNullOrEmpty())
            {
                response.Msg = "请选择试压类型";
                return response;
            }
            if (obj.Company.IsNullOrEmpty())
            {
                response.Msg = "请输入施工单位";
                return response;
            }
            if (obj.ManometerPrecision.IsNullOrEmpty())
            {
                response.Msg = "请选择压力表精度";
                return response;
            }
            if (obj.ManometerRange.IsNullOrEmpty())
            {
                response.Msg = "请选择压力表量程";
                return response;
            }

            if (obj.TemplateID.IsNullOrEmpty())
            {
                response.Msg = "请选择试压模板";
                return response;
            }
            bool res = ProjectModel.RecordExistenceWithNumber(obj.Number);
            if (res)
            {
                response.Msg = string.Format("【{0}】工程已存在", obj.Number);
                return response;
            }
            List<ProjectModel> list = new List<ProjectModel>();
            list.Add(obj);
            int result = SqlSugarHelper.Insertable(list);
            if (result > 0)
            {
                response.Code = 200;
                response.Msg = string.Format("已添加【{0}】工程", obj.Number);
            }
            else
            {
                response.Msg = string.Format("工程添加失败");
            }
            return response;
        }
        /// <summary>
        /// 删除试压工程
        /// </summary>
        /// <param name="number"></param>
        /// <returns></returns>
        public static Response DeleteProject(string number)
        {
            Response response = Response.DefaultResponse();
            Dictionary<string, Object> where = new Dictionary<string, Object>();
            where["number"] = number;
            List<ProjectModel> list = SqlSugarHelper.Inquireable<ProjectModel>(where);
            if (list.Count > 0)
            {
                int deleteRes = SqlSugarHelper.Deleteable(list);
                if (deleteRes > 0)
                {
                    response.Code = 200;
                    response.Msg = string.Format("试压工程已删除");
                }
                else
                {
                    response.Msg = string.Format("试压工程删除失败");
                }
                return response;
            }
            else
            {
                response.Msg = string.Format("【{0}】试压工程不存在，无法删除", number);
            }
            return response;
        }

        /// <summary>
        /// 试压工程列表
        /// </summary>
        /// <param name="page">页码</param>
        /// <param name="pageSize">每页数量</param>
        /// <returns></returns>
        public static List<ProjectModel> ProjectList(int page, int pageSize, ref int totalCount)
        {
            List<ProjectModel> list = SqlSugarHelper.InquireableToPageList<ProjectModel>(null, page, pageSize, ref totalCount);
            return list;
        }
    }
}
